<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once("../../../app/_inc/include.php");
require_once("./_inc/layoutFunction.php");

$errMsg = "";
if(isset($_REQUEST["reqtype"]) && $_REQUEST["reqtype"] == "save"){
	$errMsg .= layoutAliasIns();
}
//load entity
$sSQL = "SELECT * FROM entity ORDER BY entity";
$objSDAO->query($sSQL, "ent");

//alias available
$sLet = "ABCDEFGHIJKLMNOPQ";
$sSQL = "SELECT layout_alias FROM layout_alias WHERE layout = '".sxRAS('layout')."'";
$objSDAO->query($sSQL,'alidis');
while($rowAD=$objSDAO->fetch('alidis')) {
	$sLet = str_replace($rowAD['layout_alias'],'',$sLet);
}

$aOpz = array("kmenu"=>'pro.mod', "isReloadable"=>$isReloadable);
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript">
function objFrmSubm(rt){
	document.objFrm.reqtype.value = rt;
	document.objFrm.submit();
}
</script>
  
<?php
echo(bodyHTML($aOpz));

echo getLOHeader($layout);

$aCmd = array("insMD"=>array("pag"=>"javascript:objFrmSubm('save');",
										"title"=>_t("save"),
										"text"=>_t("save")
										),
					"back"=>array("pag"=>$objUser->getHistory(0, false),
										"title"=>$objUser->getHistoryText(0),
										"text"=>$objUser->getHistoryText(0)
										)
					);
echo(commandsHTML($aCmd));	

echo getHtmlForm('objFrm');
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('reqtype','save');
echo getHtmlHidden('layout');

echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" width=\"20%\" colspan='2'>Alias</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Alias*</td>" .
			"<td class=\"text01\">");
echo("<select class=\"inpt01\" name=\"layout_alias\" id=\"layout_alias\">");
for($i = 0; $i < strlen($sLet); $i++){
	$sSel = (sxREQ('layout_alias')==$sLet[$i])?'SELECTED':'';
	echo("<option value=\"".$sLet[$i]."\" $sSel>".$sLet[$i]."</option>");
}
echo("</select>");	
echo("</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Entity*</td>" .
			"<td class=\"text01\">");
echo("<select class=\"inpt01\" name=\"entity\" id=\"entity\">");
echo("<option value=\"\">-----</option>");
while($row = $objSDAO->fetch("ent")){
	$sSel = ($entity == $row["entity"])?"SELECTED":'';
	echo("<option value=\"".$row["entity"]."\" $sSel>".$row["entity"]."</option>");
}
echo("</select>");
echo("</td>" .
		"</tr>\n");
echo("</table>");

echo(commandsHTML($aCmd));

echo("</form>");
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());

 function layoutAliasIns($redirOnEnd = true){
 	global $objSDAO;
	$errMsg = "";
	if(sxREQUEST("layout_alias") == "")		$errMsg .= _t("alias missing").".<br>";
	if(sxREQUEST("entity") == "")		$errMsg .= _t("entity missing").".<br>";
	
	//check existence
	$sSQL = "SELECT count(*) as num FROM layout_alias " .
			" WHERE layout='".sxRAS("layout")."' ".
			" AND layout_alias='".sxRAS("layout_alias")."'";
	$objSDAO->query($sSQL,"test");
	$row = $objSDAO->fetch("test");
	if($row["num"] != 0)		$errMsg .= _t("alias existing")."<br>";
	
	if($errMsg != ""){ 
		return $errMsg; 
	}	
	$sSQL = "INSERT INTO " .
			"layout_alias " .
			"(layout, layout_alias, entity)" .
			"VALUES('".sxRAS("layout")."', " .
			"'".sxRAS("layout_alias")."', " .
			"'".sxRAS("entity")."')";	
	$objSDAO->query($sSQL,"ins");
	$url = $_SESSION[USEROBJ]->getHistory(0, true);
	serverRedir($url);
	return 'abnormal end';
}
